import os
import time
from unittest import result
# 添加当前目录到Python路径
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

# 然后导入config
from .config import appkey, secret1
import execjs

from toolss import getSign, md5_hash
def getResult1(userToken1,data):
    current_dir = os.path.dirname(os.path.abspath(__file__))
    # 构建 jkUtils.js 的绝对路径
    jk_utils_path = os.path.join(current_dir, '../js/jkUtils.js')
    # 读取JavaScript文件内容
    with open(jk_utils_path, 'r', encoding='utf-8') as file:
        js_code = file.read()
    # 创建JavaScript执行环境
    ctx = execjs.compile(js_code)
    result = ctx.call('jkGetSign2', data, None, None, None, userToken1)
    sign = md5_hash(result['sign']).upper();
    result['sign'] = sign
    return result;

def getResult2(userToken1,data):
    timestamp = time.time()
    result = data
    # 转换为毫秒级时间戳（13 位）
    timestamp_ms = int(timestamp * 1000)
    result['timestamp'] = timestamp_ms
    result['access_token'] = userToken1
    result['appkey'] = appkey
    secret = secret1
    result['sign'] = getSign(data, result, secret);
    return result;